<?php

// +----------------------------------------------------------------------
// | H1CMS © OpenSource CMS
// +----------------------------------------------------------------------
// | Copyright (c) 2014-2016 http://www.h1cms.com All rights reserved.
// | Copyright (c) 2014-2016 嘉兴领格信息技术有限公司，并保留所有权利。
// +----------------------------------------------------------------------
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------
// | Author: Allen <allen@lg4.cn>
// +----------------------------------------------------------------------

/**
 * Description of LG
 *
 * @author Allen <allen@lg4.cn>
 */
class LG {

    protected static $data = [];

    public static function Action($action = 'action') {
        $method = Lg\Http\Input::query($action, 'index') . 'Action';
        $initialize_method = module_name();
        if (!empty($initialize_method)) {
            $initialize_method .= '_initialize';
            if (function_exists($initialize_method))
                call_user_func($initialize_method);
        }
        if (function_exists('initialize'))
            call_user_func('initialize');
        if (function_exists($method)) {
            LG::set('lg_call_action_name', strtolower(Lg\Http\Input::query($action, 'index')));
            $return = call_user_func($method);
            if ($return instanceof \Symfony\Component\HttpFoundation\Response) {
                $return->setCharset(config('config.charset', 'utf-8'));
                $return->send();
            } else if ($return instanceof \Lg\View\View) {
                $response = h1cms\H1::$app->resp->setContent($return);
                $response->setCharset(config('config.charset', 'utf-8'));
                $response->send();
            } else if (is_string($return)) {
                $response = h1cms\H1::$app->resp->setContent($return);
                $response->setCharset(config('config.charset', 'utf-8'));
                $response->send();
            } else {
                echo $return;
            }
        } else if (function_exists('errorAction')) {
            call_user_func_array('errorAction', array(404, 'Page not found'));
        } else {
            LG::set('lg_call_action_name', '');
        }
    }

    public static function with($key, $value = null) {
        if (is_array($key)) {
            static::$data = array_merge(static::$data, $key);
        } else {
            static::$data[$key] = $value;
        }
    }

    public static function set($key, $value) {
        static::$data[$key] = $value;
    }

    public static function get($key) {
        if (isset(static::$data[$key])) {
            return static::$data[$key];
        }
        return NULL;
    }

    public static function has($key) {
        return array_key_exists($key, static::$data);
    }

    public static function setAlias($key, $value) {
        static::$data['_alias_' . $key] = $value;
    }

    public static function getAlias($key, $default = null) {
        if (strncmp($key, '@', 1)) {
            return $key; //no alias
        }
        $pos = strpos($key, '/');
        $root = $pos === false ? $key : substr($key, 0, $pos);
        if (isset(static::$data['_alias_' . $root]) && is_string(static::$data['_alias_' . $root])) {
            return $pos === false ? static::$data['_alias_' . $root] : static::$data['_alias_' . $root] . substr($key, $pos);
        }
        return $default;
    }

    public static function hasAlias($key) {
        return array_key_exists('_alias_' . $key, static::$data);
    }

    public static function basePath() {
        if (static::has('__basePath')) {
            return static::get('__basePath');
        }
        $documentRoot = Lg\Http\Request::classic()->server('DOCUMENT_ROOT');
        $rootDirName = basename($documentRoot);
        $appDirName = basename(APP_PATH);
        if ($rootDirName == $appDirName) {
            static::set('__basePath', '');
        } else {
            static::set('__basePath', str_replace($documentRoot, '', str_replace('\\', '/', APP_PATH)));
        }
        return static::get('__basePath');
    }

    public static function getVersion() {
        return LG_VERSION;
    }

}
